package com.example.rabbitmqdemo.hellorabbitmq.simplest;

import com.example.rabbitmqdemo.hellorabbitmq.CommonUtil;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Slf4j
public class ConsumerSimplest {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = CommonUtil.getFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                log.info("-- cunsumerTag : {}", consumerTag);
                log.info("-- exchange : {}", envelope.getExchange());
                log.info("-- routingKey : {}", envelope.getRoutingKey());
                log.info("-- boday : {}", new String(body));
            }
        };
        channel.basicConsume("simplest", defaultConsumer);
    }
}
